function GameObject2(id) {
    this.x = 0;
    this.y = 0;
    this.id = id;

}
GameObject2.method('setID', function (value) {
    this.id = value;
    return this;
});

GameObject2.method('getID', function () {
    return this.id;
});

GameObject2.method('setPos', function (x_abs, y_abs) {
    this.x = x_abs * grid;
    this.y = y_abs * grid;
});

GameObject2.method('getPosX', function () {
    return Math.ceil(this.x / grid);

});
GameObject2.method('getPosY', function () {
    return Math.ceil(this.y / grid);

});
GameObject2.method('draw', function() {

    try {
        if (this instanceof Player2) {
            ctx.drawImage(images["TilesSprite2"], grid * 1, 0, grid, grid, this.x, this.y, grid, grid);
        }
        else if (this instanceof Bomb2) {
            if (this.timer < this.delay - this.animationDuration) {
                ctx.drawImage(images["spritesheet1"], 0, maxGrid * 2, maxGrid, maxGrid, this.x - offSet, this.y- offSet, maxGrid, maxGrid);
            }
            else {
                this.explosionAnimation();
            }
        }
    } catch(e) {
        alert(e);
    }
});


function Player2(id) {
    this.setID(id);
    this.x_move = 0;
    this.y_move = 0;
    this.type = PlayerType.HUMAN;
    this.speed = 10;
    this.lives = 3;
    this.kills = 0;
    this.maxBombsNumber = 3;
    this.currentBombsNumber = 0;
    this.assingedKeys = null;
}

Player2.inherits(GameObject2);

Player2.method('moveX', function (n) {
    this.x_move += n * grid;
});

Player2.method('moveY', function (n) {
    this.y_move += n * grid;
});

Player2.method('isMoving', function () {
    return (this.x_move != 0 || this.y_move != 0);
});

Player2.method('isAlive', function () {
    return this.lives > 0;

});
Player2.method('bombExploded', function () {
    this.currentBombsNumber--;
});
Player2.method('getActiveBombsCount', function () {
    return this.currentBombsNumber;
});
Player2.method('removeLife', function () {
    if (!this.lives == 0) {
        this.lives--;
        updatePlayerSidebar(this);
    }

});

Player2.method('addKills', function () {
    this.kills++;
    updatePlayerSidebar(this);
});
Player2.method('isCurrentPlaceFree', function () {
    result = getItemIndexByPos(bombs, this.getPosX(), this.getPosY());
    return result == -1 ? true : false;
});
Player2.method('plantBomb', function () {
    if (this.isCurrentPlaceFree()) {
        // undefined bombs will be overwrote by active bomb
        var bombAdded = false;
        result = bombs.indexOf(undefined);
        if (result != -1) {
            bombs[result] = new Bomb2(this.getPosX(), this.getPosY(), this);
            bombAdded = true;
        }
        if (!bombAdded) {
            bombs.push(new Bomb2(this.getPosX(), this.getPosY(), this));
        }
        this.currentBombsNumber++;
    }
});
Player2.method('assignKeys', function (keys) {
    this.assingedKeys = keys;
});
Player2.method('update', function () {
    if (this.x_move > 0) {
        this.x_move -= this.speed;
        this.x += this.speed;
        if (this.x_move < 0) {
            this.x += this.x_move;
            this.x_move = 0;
        }
    }
    if (this.x_move < 0) {
        this.x_move += this.speed;
        this.x -= this.speed;
        if (this.x_move > 0) {
            this.x += this.x_move;
            this.x_move = 0;
        }
    }
    if (this.y_move > 0) {
        this.y_move -= this.speed;
        this.y += this.speed;
        if (this.y_move < 0) {
            this.y += this.y_move;
            this.y_move = 0;
        }
    }
    if (this.y_move < 0) {
        this.y_move += this.speed;
        this.y -= this.speed;
        if (this.y_move > 0) {
            this.y += this.y_move;
            this.y_move = 0;
        }
    }
});



